package com.mng.index12306.framework.starter.web;

import cn.hutool.core.util.StrUtil;
import com.mng.index12306.framework.starter.convention.errorcode.BaseErrorCode;
import com.mng.index12306.framework.starter.convention.exception.AbstractException;
import com.mng.index12306.framework.starter.convention.result.Result;
import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.parser.HttpParser;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import cn.hutool.core.collection.CollectionUtil;

import java.util.Collections;
import java.util.Optional;

/**
 * @Description : 全局异常处理器
 * @Author : YuShaochao
 * @Date : 2024-05-08 12:07
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 拦截参数验证异常
     */
    @SneakyThrows
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public Result validExceptionHandler(HttpServletRequest request, MethodArgumentNotValidException ex){
        BindingResult bindingResult = ex.getBindingResult();
        FieldError fieldError = CollectionUtil.getFirst(bindingResult.getFieldErrors());

        String exceptionStr = Optional.ofNullable(fieldError)
                .map(FieldError::getDefaultMessage)
                .orElse(StrUtil.EMPTY);

        log.error("[{}] {} [ex] {}",request.getMethod(), getUrl(request), exceptionStr);

        return Results.failure(BaseErrorCode.CLIENT_ERROR.code(),exceptionStr);

    }

    /**
     * 拦截应用内抛出的异常
     */
    @ExceptionHandler(value = AbstractException.class)
    public Result abstractException(HttpServletRequest request,AbstractException ex){
        if (ex.getCause() != null) {
            log.error("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex.toString(), ex.getCause());
            return Results.failure(ex);
        }
        log.error("[{}] {} [ex] {}", request.getMethod(), request.getRequestURL().toString(), ex.toString());
        return Results.failure(ex);
    }

    /**
     * 拦截未捕获的异常
     */
    @ExceptionHandler(value = Throwable.class)
    public Result defaultErrorHandler(HttpServletRequest request, Throwable throwable){
        log.error("[{}] {} ",request.getMethod(),getUrl(request),throwable);
        return Results.failure();
    }

    private String getUrl(HttpServletRequest request) {
        if(StringUtils.isEmpty(request.getQueryString())){
            return request.getRequestURL().toString();
        }
        return request.getRequestURL().toString() + "?" + request.getQueryString();
    }
}
